package work.jame.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import work.jame.blog.configuration.security.LoginUser;
import work.jame.blog.framework.utils.security.SpringSecurityUtils;
import work.jame.blog.pojo.ArticleTag;
import work.jame.blog.mapper.ArticleTagMapper;
import work.jame.blog.service.ArticleTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jame
 * @since 2023-05-17
 */
@Service
public class ArticleTagServiceImpl extends ServiceImpl<ArticleTagMapper, ArticleTag> implements ArticleTagService {

    @Override
    public void saveOrQueryArchiveTag(List<ArticleTag> tags) {
        for (ArticleTag tag : tags) {
            QueryWrapper<ArticleTag> wrapper = new QueryWrapper<>();
            wrapper.eq("tag_name", tag.getTagName());
            ArticleTag dbTag = this.getOne(wrapper);
            if (Objects.isNull(dbTag)) {
                Long userId  = SpringSecurityUtils.getCurrentLoginUserId();
                tag.setAddUserId(userId);
                this.save(tag);
            } else {
                tag.setId(dbTag.getId());
            }
        }
    }
}
